package com.example.imtoken.modules.user.create

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.InjectorUtils
import com.example.imtoken.databinding.ActivityCreateUserBinding
import com.example.imtoken.modules.main.wallet.add_token.AddTokenActivity

/**
 * 创建身份
 */
class CreateUserActivity :
    BaseActivity<CreateUserViewModel, ActivityCreateUserBinding>(CreateUserViewModel::class.java) {

    private var isPwShow = false //密码是否可见

    private val viewModelF by lazy {
        getViewModelFactory(InjectorUtils.getCreateUserViewModel())
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_create_user
    }

    override fun init() {
        dataBinding.headView.ivHeadBack.setOnClickListener { finish() }
        dataBinding.headView.vLine.visibility = View.INVISIBLE
        dataBinding.ivPassShow.setOnClickListener {
            //隐藏键盘
            if (KeyboardUtils.isSoftInputVisible(this)) {
                KeyboardUtils.hideSoftInput(this)
            }
            etViewClearFocus()
            //取消重复输入密码焦点
            dataBinding.etPassWord2.clearFocus()
            //密码的显示与隐藏关
            if (isPwShow) {
                dataBinding.etPassWord1.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
                dataBinding.etPassWord2.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
                dataBinding.ivPassShow.setImageResource(R.mipmap.ic_password_show)
            } else {
                dataBinding.etPassWord1.transformationMethod =
                    PasswordTransformationMethod.getInstance()
                dataBinding.etPassWord2.transformationMethod =
                    PasswordTransformationMethod.getInstance()
                dataBinding.ivPassShow.setImageResource(R.mipmap.ic_password_no_show)
            }
            isPwShow = !isPwShow
        }

        dataBinding.viewModel = viewModelF

        initObserve()
        initClickView()
    }

    private fun initClickView() {
        dataBinding.tvCreate.setOnClickListener {
            viewModelF.createUser3()
        }
    }

    private fun initObserve() {
        viewModelF.realName.observe(this) { viewModelF._realName = it }
        viewModelF.passWord1.observe(this) { viewModelF._passWord1 = it }
        viewModelF.passWord2.observe(this) { viewModelF._passWord2 = it }
        viewModelF.hint.observe(this) { viewModelF._hint = it }
        viewModelF.isSubmit.observe(this) {
            if (it) {
                dataBinding.tvCreate.setBackgroundResource(R.drawable.shape_create_btn_bg_true)
            } else {
                dataBinding.tvCreate.setBackgroundResource(R.drawable.shape_create_btn_bg_false)
            }
        }
        viewModelF.isCreateSuccess.observe(this) {
            if (it) {
                //跳转添加代币
                ActivityUtils.startActivity(AddTokenActivity::class.java)
            }
        }
    }

    /**
     * 清除输入框的焦点
     */
    private fun etViewClearFocus() {
        dataBinding.etName.clearFocus()
        dataBinding.etPassWord1.clearFocus()
        dataBinding.etPassWord2.clearFocus()
        dataBinding.etTips.clearFocus()
    }

}